package com.gitee.etl.bean

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.serializer.SerializerFeature
import nl.basjes.parse.httpdlog.HttpdLoglineParser

class LogBean {

  //用户id信息
  var _connectionClientUser: String = _

  def setConnectionClientUser(value: String): Unit = {
    _connectionClientUser = value
  }

  def getConnectionClientUser = {
    _connectionClientUser
  }

  //ip地址
  private[this] var _ip: String = _

  def setIp(value: String): Unit = {
    _ip = value
  }

  def getIp = {
    _ip
  }

  //请求时间
  private[this] var _requestTime: String = _

  def setRequestTime(value: String): Unit = {
    _requestTime = value
  }

  def getRequestTime = {
    _requestTime
  }

  //请求方式
  private[this] var _method: String = _

  def setMethod(value: String) = {
    _method = value
  }

  def getMethod = {
    _method
  }

  //请求资源
  private[this] var _resolution: String = _

  def setResolution(value: String) = {
    _resolution = value
  }

  def getResolution = {
    _resolution
  }

  //请求协议
  private[this] var _requestProtocol: String = _

  def setRequestProtocol(value: String): Unit = {
    _requestProtocol = value
  }

  def getRequestProtocol = {
    _requestProtocol
  }

  //响应码
  private[this] var _responseStatus: Int = _

  def setRequestStatus(value: Int): Unit = {
    _responseStatus = value
  }

  def getRequestStatus = {
    _responseStatus
  }

  //返回的数据流量
  private[this] var _responseBodyBytes: String = _

  def setResponseBodyBytes(value: String): Unit = {
    _responseBodyBytes = value
  }

  def getResponseBodyBytes = {
    _responseBodyBytes
  }

  //访客的来源url
  private[this] var _referer: String = _

  def setReferer(value: String): Unit = {
    _referer = value
  }

  def getReferer = {
    _referer
  }

  //客户端代理信息
  private[this] var _userAgent: String = _

  def setUserAgent(value: String): Unit = {
    _userAgent = value
  }

  def getUserAgent = {
    _userAgent
  }

  //跳转过来页面的域名:HTTP.HOST:request.referer.host
  private[this] var _referDomain: String = _

  def setReferDomain(value: String): Unit = {
    _referDomain = value
  }

  def getReferDomain = {
    _referDomain
  }

  override def toString: String = {
    JSON.toJSONString(this,SerializerFeature.DisableCircularReferenceDetect)
  }

}

object LogBean {
  //1：定义点击流日志的解析规则
  // 根据apache的HTTPD手册定义解析规则
  val getLogFormat: String = "%u %h %l %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""

  //接收字符串日志,返回日志对象
  def apply(parser: HttpdLoglineParser[LogBean], logline: String): LogBean = {
    val bean = new LogBean
    parser.parse(bean,logline)
  }

  def createLogParse(): HttpdLoglineParser[LogBean] = {
    val parser = new HttpdLoglineParser[LogBean](classOf[LogBean], getLogFormat)

    //建立bean对象属性与参数名的映射关系
    parser.addParseTarget("setConnectionClientUser", "STRING:connection.client.user")
    parser.addParseTarget("setIp", "IP:connection.client.host")
    parser.addParseTarget("setRequestTime", "TIME.STAMP:request.receive.time")
    parser.addParseTarget("setMethod", "HTTP.METHOD:request.firstline.method")
    parser.addParseTarget("setResolution", "HTTP.URI:request.firstline.uri")
    parser.addParseTarget("setRequestProtocol", "HTTP.PROTOCOL_VERSION:request.firstline.protocol")
    parser.addParseTarget("setResponseBodyBytes", "BYTES:response.body.bytes")
    parser.addParseTarget("setReferer", "HTTP.URI:request.referer")
    parser.addParseTarget("setUserAgent", "HTTP.USERAGENT:request.user-agent")
    parser.addParseTarget("setReferDomain", "HTTP.HOST:request.referer.host")

    parser
  }

  /*测试*/
  def main(args: Array[String]): Unit = {
    //2：指定解析的字符串
    val logline = "2001:980:91c0:1:8d31:a232:25e5:85d 222.68.172.190 - [05/Sep/2010:11:27:50 +0200] \"GET /images/my.jpg HTTP/1.1\" 404 23617 \"http://www.angularjs.cn/A00n\" \"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; nl-nl) AppleWebKit/533.17.8 (KHTML, like Gecko) Version/5.0.1 Safari/533.17.8\""
    val bean = new LogBean
    val parser: HttpdLoglineParser[LogBean] = LogBean.createLogParse()
    val beanlog: LogBean = parser.parse(bean, logline)

    println(beanlog)
  }
}
